/**
 * @param {string} str1
 * @param {string} str2
 * @return {string}
 */
var gcdOfStrings = function(str1, str2) {
     
    let minLength = Math.min(str1.length, str2.length);
    let maxString = '';
    for (let i = 0; i < minLength; i++) {
        if (str1[i]!== str2[i]) break;
        maxString += str1[i];
    }
    for (let i = maxString.length; i > 0; i--) {
        const target = maxString.slice(0, i);
        if (validate(target, str1, str2)) return target;
    }
    return "";
};

const validate = (target,...string) => {
    return string.every(str => {
        const times = str.length / target.length;
        return Number.isInteger(times) && target.repeat(times) === str;
    });
};

console.log(gcdOfStrings("ABCABC", "ABC"));
